/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-web-praise nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.lpitools.gwtgui.client.components.treeutil;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.view.client.TreeViewModel;
import com.sri.ai.lpitools.cobra.gwtutil.dataobjects.shared.TreeNode;
import com.sri.ai.lpitools.cobra.gwtutil.dataobjects.shared.TreeNode.NodeType;
import com.sri.ai.lpitools.cobra.gwtutil.dataobjects.shared.TreeNode.TreeType;

/**
 * This class does most of the backend work to create the trace tree.
 * @author dabraham
 *
 */
public class TraceCellTreeModel {
	
	private static String clientID;
	
	public static class TraceTreeModel implements TreeViewModel {

		/**
		 * This defines a method for getting the children of a node.
		 */
		public <T> NodeInfo<?> getNodeInfo(T value) {
			TreeNode val = (TreeNode) value;
			//get this node's ID.
			String valID = val.getID();
			//get all children with this parent ID
			TreeNodeDataProvider dataProvider = new TreeNodeDataProvider(valID, clientID, TreeType.TRACE);
			//define a cell.
			TraceCell cell = new TraceCell();
			return new DefaultNodeInfo<TreeNode>(dataProvider,cell);
		}

		/**
		 * Returns whether the node is a leaf or not.
		 */
		public boolean isLeaf(Object value) {
			TreeNode val = (TreeNode) value;
			return val.isLeaf();
		}
		
	}
	
	/**
	 * cell pattern for trace tree
	 */
	public static class TraceCell extends AbstractCell<TreeNode>{

		interface Templates extends SafeHtmlTemplates {
			@SafeHtmlTemplates.Template("<div class=\"{0}\">{1}</div>")
			SafeHtml cell(String classname, SafeHtml value);
		}
		
		private static Templates templates = GWT.create(Templates.class);
		/**
		 * Builds the html for the tree cell
		 */
		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				TreeNode value, SafeHtmlBuilder sb) {
			//get this node's Type and determine classname
			String valType = value.getColor();
			String classname = "";
			if(valType.equalsIgnoreCase("TERMINAL")) {
				classname = "gwt-TreeItem-Terminal"; //should be colored gray
			} else if(valType.equalsIgnoreCase("SYMBOL")) {
				classname = "gwt-TreeItem-Symbol"; //should be colored blue
			} else if(valType.equalsIgnoreCase("STRING")) {
				classname = "gwt-TreeItem-String"; //should be colored brown
			} else {
				classname = "gwt-TreeItem-Other"; //should be colored black
			}	
			SafeHtml safeValue = SafeHtmlUtils.fromString(value.getLabel());
			SafeHtml rendered = templates.cell(classname, safeValue);

			sb.append(rendered);
		}
	};
	
	/**
	 * generates the tree defined by this model.
	 */
	public static CellTree getTree(String clientID) {
		TraceCellTreeModel.clientID = clientID;
		TreeNode rootObj = new TreeNode("0", "ROOT", TreeType.TRACE, NodeType.STRING, false);
		return new CellTree(new TraceTreeModel(), rootObj);
	}
}
